<%@ page import="com.wy.util.DBUtil" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="com.wy.util.DBUtil" %>
<%@ page import="com.wy.util.MD5Util" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
    /*将用户录入界面填写的内容获得过来，保存到用户表tb_user中*/
    //1.获得表单传递的内容
    String nick_name  = request.getParameter("nickName");
    String uname  = request.getParameter("username");
    String pwd  = request.getParameter("userPwd");
    String passwor= MD5Util.encrypt(pwd);
    //2.设置新增用户的sql语句
    String sql="INSERT INTO news_user(username, user_pwd, nick_name) VALUES('" + uname + "', '" + passwor + "', '" + nick_name + "')";

    //3.执行sql
    DBUtil dbUtil  = new DBUtil();
    //检测输入的用户名是否已经存在
    String sql2=
            "select * from news_user where username='"+uname+"' ";
    String sql3=
            "select *,nick_name as nickName from news_user where nick_name='"+nick_name+"' ";
    ResultSet rs  = dbUtil.query(sql2);
    ResultSet rs1  = dbUtil.query(sql3);

    if( rs.next()){
        out.print("输入的用户名已经存在!");
    }
    else if(rs1.next()){
        out.print("输入的名称已经存在!");
    }
    else {
        int n = dbUtil.update(sql);
        if (n > 0)    //4.判断结果
            response.sendRedirect("/login/login.jsp");
        else
            out.print("增加失败!");
    }
%>